package com.sm.li.controller;

import com.github.pagehelper.PageHelper;
import com.sm.li.common.http.AxiosResult;
import com.sm.li.common.http.AxiosStatus;
import com.sm.li.common.page.PageBean;
import com.sm.li.common.perm.HasPerm;
import com.sm.li.common.valid.group.AddGroup;
import com.sm.li.common.valid.group.UpdateGroup;
import com.sm.li.controller.base.BaseController;
import com.sm.li.dto.BrandDTO;
import com.sm.li.entity.Brand;
import com.sm.li.query.BrandQuery;
import com.sm.li.service.BrandService;
import com.sm.li.transfer.BrandTransfer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.BindResult;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("brand")
@Api(tags="品牌管理",description = "品牌管理")
public class BrandController extends BaseController {
    @Autowired
    private BrandService brandService;

    @Autowired
    private BrandTransfer brandTransfer;

        @GetMapping
    @ApiOperation(value = "查询所有")
    public AxiosResult<List<Brand>> list() {
        return AxiosResult.success(brandService.list());
    }
    @GetMapping("{id}")
    @ApiOperation(value = "id查询")
    public AxiosResult<BrandDTO> findById(@PathVariable Long id) {
        return AxiosResult.success(brandTransfer.toDTO(brandService.findById(id)));
    }

    //分页条件查询
    @GetMapping("searchPage")
    public AxiosResult<PageBean<BrandDTO>> searchPage(BrandQuery brandQuery) {
        //开启分页
        System.out.println(brandQuery);
        PageHelper.startPage(brandQuery.getCurrentPage(), brandQuery.getPageSize());
        PageBean<BrandDTO> pageBean = brandService.searchPage(brandQuery);
        return AxiosResult.success(pageBean);
    }

    //@Valid 开启后端表单校验
    @PostMapping
    @HasPerm(permSign = "goods:brand:add")
    @ApiOperation(value = "添加")
    public AxiosResult add(@Validated(AddGroup.class) @RequestBody Brand brand) {

        return toAxios(brandService.add(brand));
    }

    @PutMapping
    @HasPerm(permSign = "goods:brand:edit")
    @ApiOperation(value = "修改")
    public AxiosResult<Void> update(@Validated(UpdateGroup.class) @RequestBody Brand brand) {
        return toAxios(brandService.update(brand));

    }

    @DeleteMapping("{id}")
    @HasPerm(permSign = "goods:brand:delete")
    @ApiOperation(value = "通过id删除")
    public AxiosResult<Void> deleteById(@PathVariable Long id) {
        return toAxios(brandService.deleteById(id));

    }

    @DeleteMapping("batch/{ids}")
    @HasPerm(permSign = "goods:brand:batch")
    @ApiOperation(value = "批量删除")
    public AxiosResult<Void> batchDeleteById(@PathVariable List<Long> ids) {
        return toAxios(brandService.deleteBatchIds(ids));

    }

}
